package com.yilin.tms.user.controller;

import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.user.RoleType;
import com.yilin.tms.core.entity.user.account.Account;
import com.yilin.tms.core.entity.user.organize.UserRole;
import com.yilin.tms.user.service.IAccountService;
import com.yilin.tms.user.service.IRoleService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色管理
 */
@RestController
@RequestMapping("/user/role/")
public class RoleController extends BaseController {

    @Resource
    private IRoleService roleService;
    @Resource
    private IAccountService accountService;

    /**
     * 获取角色列表
     */
    @PostMapping("getRoleByOrganizeId")
    public ReturnData<List<UserRole>> getRoleByOrganizeId(String organizeId,UserRole query) {
        if (organizeId == null) organizeId = getLoginUser().getOrganizeId();
        List<UserRole> roleList = roleService.getRoleListByOrganizeId(organizeId,query);
        return jsonView(true, "获取角色列表成功", roleList);
    }

    /**
     * 保存角色
     */
    @PostMapping("saveEmployeeRole")
    public ReturnData<Object> saveEmployeeRole(UserRole userRole) {
        ParamUtil.isNull(userRole.getName(),"角色名称不能为空！");
        ParamUtil.isNull(userRole.getRoleJurisdiction(),"角色权限不能为空！");
        ParamUtil.notEquals("你没有权限操作角色！",getLoginUser().getRoleType(),RoleType.manager);
        if (userRole.getId()==null){
            UserRole localUserRole = roleService.getEmployeeRoleByName(getLoginUser().getOrganizeId(), userRole.getName());
            if (localUserRole != null) {
                return jsonView(false, "角色名称重复！");
            }
        }
        ParamUtil.isNull(getLoginUser().getOrganizeName(),"你的组织信息未完善，请完善组织名称后操作！");
        if (userRole.getRoleType() == null){
            userRole.setRoleType(RoleType.employee);
        }
        ParamUtil.equals("角色类型不支持注册！",userRole.getRoleType(),RoleType.manager);
        userRole.setStatus(true);
        userRole.setManager(false);
        userRole.setOrganizeId(getLoginUser().getOrganizeId());
        userRole.setOrganizeName(getLoginUser().getOrganizeName());
        roleService.saveOrUpdate(userRole);
        return jsonView(true, "保存成功！");
    }

    /**
     * 删除角色 配置数据直接删除
     * 需检查 账户信息
     */
    @PostMapping("deleteEmployeeRole")
    public ReturnData<Object> deleteEmployeeRole(UserRole userRole) {
        ParamUtil.isNull(userRole.getId(),"未指定数据！");
        UserRole role = roleService.getById(userRole.getId());
        ParamUtil.equals("管理员角色不能被删除！",role.getRoleType(),RoleType.manager);
        List<Account> accounts = accountService.getAccountByRoleId(userRole.getId());
        if (!accounts.isEmpty()) {
            return jsonView(false, "角色下存在用户，无法删除！");
        }
        roleService.removeById(userRole);
        return jsonView(true, "删除成功！");
    }

    /**
     * 查看使用该角色的账户列表
     */
    @PostMapping("getAccountByRoleId")
    @NotNull("roleId")
    public ReturnData<Object> getAccountByRoleId( @Explain("角色ID")String roleId) {
        List<Account> accounts = accountService.getAccountByRoleId(roleId);
        return jsonView(true, "获取成功！",accounts);
    }

}
